﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOFrameworks;
using PhysicalDataProvider;
using System.Xml;

namespace FAQFrameworks.Framewors
{
    /*
    public interface ITopicManager
    {
        IDataElement CreateTopic
            (string topicTitle, int intForumId, string remark);

        Guid AddTopic(IDataElement topic);

        IDataElement GetTopic(Guid guid);
    }

    public class TopicManager : ITopicManager
    {
        DataElementSql dataProvider;

        public DataElementSql DataProvider
        {
            get { return dataProvider; }
            set { dataProvider = value; }
        }

        string[] cols =
            new string[] { "TopicId", "Title", "EventId", "Status", "Content", "ForumId" };

        string[] idCols = new string[] { "TopicId", "EventId", "ForumId" };

        #region ITopicManager 成员

        public IDataElement CreateTopic
            (string topicTitle, int intForumId, string remark)
        {
            XmlDocument doc = new XmlDocument();

            XmlElement topicEle = doc.CreateElement("Topic");
            doc.AppendChild(topicEle);

            XmlElement titleEle = doc.CreateElement("Title");
            topicEle.AppendChild(titleEle);

            XmlElement contentEle = doc.CreateElement("Content");
            contentEle.InnerXml = topicTitle;
            titleEle.AppendChild(contentEle);

            XmlElementDataElement de
                = new XmlElementDataElement(topicEle);

            de["@status"].Value = TopicStatus.Asked.ToString();

            de["@ForumId"].Value = intForumId;

            if (!string.IsNullOrEmpty(remark))
            {
                de["Remarks"]["Content"].Value = remark;
            }

            return de;
        }

        public Guid AddTopic(IDataElement topic)
        {
            Guid topicId = Guid.NewGuid();
            topic["TopicId"]
                = new ObjectDataElement(Convert.ToBase64String(topicId.ToByteArray()));

            IEventManager eventManager = new EventManager();
            Guid guid = eventManager.AddEvent(de);

            TopicDbDataElement de = new TopicDbDataElement(topic);

            DataProvider.Insert(de,
                cols,
                "t_Topics", idCols);

            return topicId;
        }

        public IDataElement GetTopic(Guid guid)
        {
            IDataElement de = dataProvider.Select(cols, "t_Topic", "TopicId='" + Convert.ToBase64String(guid.ToByteArray()) + "'", idCols);

            string content = de["Conent"].Value as string;
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(content);

            XmlElementDataElement xmlDE
                = new XmlElementDataElement(doc.DocumentElement);

            return xmlDE;
        }

        #endregion
    }

    public class TopicDbDataElement : CustomDataElement
    {
        public TopicDbDataElement(IDataElement de)
            : base(de)
        {

        }

        public override IDataElement this[string attrName]
        {
            get
            {
                switch (attrName)
                {
                    case "Title":
                        return new StringDataElement(Convert.ToString(base["Title/Content"].Value));

                    case "EventId":
                        string str = Convert.ToString(base["@EventId"].Value);
                        byte[] bytes = Convert.FromBase64String(str);
                        Guid guid = new Guid(bytes);
                        return new ObjectDataElement(guid);

                    case "Status":
                        return new IntDataElement(Convert.ToInt32(base["@status"].Value));

                    case "Content":
                        XmlElementDataElement de
                = base.Value as XmlElementDataElement;

                        XmlWriterSettings settings = new XmlWriterSettings();
                        settings.Indent = true;
                        settings.Encoding = Encoding.UTF8;

                        StringBuilder sb = new StringBuilder();
                        XmlWriter xmlWriter = XmlWriter.Create(sb);

                        de.Document.Save(xmlWriter);

                        xmlWriter.Close();

                        string content = sb.ToString();
                        return new StringDataElement(content);

                    case "ForumId":
                        return new IntDataElement(Convert.ToInt32(base["@ForumId"].Value));

                    default:
                        return base[attrName];
                }
            }
            set
            {
                base[attrName] = value;
            }
        }
    }

    */
}
